// global variable
window.name	= "amg-admin";
site		= "http://" + window.location.hostname + "/amg/admin/";
acid		= "";		// autocomplete id
acwidth		= 420;		// autocomplete element's width
eRR			= 0;

//-- Menu variables -------------
var timeout    = 0;
var closetimer = 0;
var menuItem = 0;
	
function dropMenu_open()	{  
	dropMenu_canceltimer();
	dropMenu_close();
	menuItem = $(this).find('ul').css('visibility', 'visible');
}

function dropMenu_close(){  
	if(menuItem) menuItem.css('visibility', 'hidden');
}

function dropMenu_timer(){  
	closetimer = window.setTimeout(dropMenu_close, timeout);
}

function dropMenu_canceltimer()
{  
	if(closetimer){
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

	
	
	
// DOM READY
$(function() {
	$('input')
		.focus(function() {
			acid	= $(this).select().addClass('focus').attr('id');
			acwidth	= $(this).width() + 18;
			$("#msg").html('&nbsp;');
		})
		.blur(function() {$(this).removeClass('focus');});

    $('tr.hover').mouseover(function() 		{$(this).children().addClass('hover')});
    $('tr.hover').mouseout (function() 		{$(this).children().removeClass('hover')});

	
	// Search input text handling on focus
	var $searchq = $("#search-q").attr("value");
	$('#search-q.text').css('color', '#999');
	$('#search-q').focus(function(){
		if ( $(this).attr('value') == $searchq) {
			$(this).css('color', '#555');
			$(this).attr('value', '');
		}
	});
	$('#search-q').blur(function(){
		if ( $(this).attr('value') == '' ) {
			$(this).attr('value', $searchq);
			$(this).css('color', '#999');
		}
	});
	
	
	// Switch categories
	$('#h-wrap').hover(function(){
			$(this).toggleClass('active');
			$("#h-wrap ul").css('display', 'block');
		}, function(){
			$(this).toggleClass('active');
			$("#h-wrap ul").css('display', 'none');
	});

	
	// Handling with tables (adding first and last classes for borders and adding alternate bgs)
	$('tbody tr:even').addClass('even');
	$('table.grid tbody tr:last-child').addClass('last');
	$('tr th:first-child, tr td:first-child').addClass('first');
	$('tr th:last-child, tr td:last-child').addClass('last');
	$('form.fields fieldset:last-child').addClass('last');
	
	
	// Handling with lists (alternate bgs)
	$('ul.simple li:even').addClass('even');

	
	// Handling with grid views (adding first and last classes for borders and adding alternate bgs)
	$('.grid .line:even').addClass('even');
	$('.grid .line:first-child').addClass('firstline');
	$('.grid .line:last-child').addClass('lastline');

	
	// Tabs switching
	$('#box1 .content#box1-grid').hide(); // hide content related to inactive tab by default
	$('#box1 .header ul a').click(function(){
		$('#box1 .header ul a').removeClass('active');
		$(this).addClass('active'); // make clicked tab active
		$('#box1 .content').hide(); // hide all content
		$('#box1').find('#' + $(this).attr('rel')).show(); // and show content related to clicked tab
		return false;
	});


	//-- Page dropdown menu
	document.onclick = dropMenu_close;
		
	$('#pgmenu > li').bind('mouseover', dropMenu_open);
	$('#pgmenu > li').bind('mouseout',  dropMenu_timer);		
	
	$('#pgmenu>li>ul,#pgmenu>li>ul>li').mouseover(function(){
		var parent = $(this).parents('li');
		$(parent).addClass('pgmv');
	});
	
	$('#pgmenu>li>ul, #pgmenu>li>ul>li').mouseout(function(){
		var parent = $(this).parents('li');
		$(parent).removeClass('pgmv');
	});

	
    $('.sortme').live('click', function(event)  {
        var id      = $(this).attr('id'),
            sortval	= this.className.split(' ')[0],
            link    = $('#pagination').attr('class').split(' ')[0],
            page    = $('#pagination').attr('class').split(' ')[1],
            limit   = $('#pagination').attr('class').split(' ')[2];

        sort       = (sortval == "na" || sortval == "desc") ? 'asc' : 'desc';
        $.get(site + link +'/ajax/'+ id + '/' + sort + '/' + page + '/' + limit, {}, function(resp) {
            $('#pagination').html(resp);
        });
    });

    $('.pageme').live('click', function(event)  {
        var link    = $(this).attr('class').split(' ')[1],
            order   = $(this).attr('class').split(' ')[2],
            sort    = $(this).attr('class').split(' ')[3],
            page    = $(this).attr('class').split(' ')[4],
            limit   = $(this).attr('class').split(' ')[5];
        $.get(site + link +'/ajax/'+ order + '/' + sort + '/' + page + '/' + limit, {}, function(resp) {
            $('#pagination').html(resp);
        });
    });

    $('.filterme').live('click', function(event)  {
        var link    = $(this).attr('class').split(' ')[1],
            order   = $(this).attr('class').split(' ')[2],
            sort    = $(this).attr('class').split(' ')[3],
            page    = $(this).attr('class').split(' ')[4],
            limit   = $(this).attr('class').split(' ')[5];
            _post = [{name:'ts', value: new Date().getTime()},
                     {name:'filter', value: $('.filter').val()}];

        $('input, select, textarea').each(function()  {
            var _obj = $(this),
                _val = $.trim(_obj.val());
        
            _post.push({name:this.id, value:_val});
        });
      
        $.post(site + link +'/ajax/'+ order + '/' + sort + '/' + page + '/' + limit, _post, function(resp) {
            $('#pagination').html(resp);
        });
    });

    $('.go').click(function () {
        var _ack  = $(this).attr('id').replace(/btnGo/g, ''),
			_post = [{name:'ts',    		value: new Date().getTime()},
					 {name:'type',			value: $('#type').val()},
					 {name:'perusahaan_uid',value: $('#perusahaan_uid').val()},
					 {name:'change_status', value: _ack}],
			_cont = window.confirm(_ack==1 ? 'Profil Perusahaan ini telah dipastikan BENAR !!!' : 'Profil Perusahaan ini telah dipastikan SALAH !!!');
        if(_cont) {
			$.post(site+'crud/', _post, function(resp) {
				window.location.href = site + 'change_profile';
			});
		}
    });

    $('.goForm').live('click', function(event)  {
        alert('goForm');
    });

    $('.goTable').live('click', function() {
        alert('goTable');
    });

    $('.goBufer').live('click', function() {
        alert('goBuffer');
    });

    $("a.del").live('click', function (event) {
        alert('del clicked')
    });

    $('#btnCancel').click(function () {
        window.location.href = site + this.className;

    });

});